package pers.qianyu.month_202101.date_20210117;

import pers.qianyu.util.model.*;

/**
 * 24. 两两交换链表中的节点
 * https://leetcode-cn.com/problems/swap-nodes-in-pairs/
 *
 * @author mizzle rain
 * @date 2021-01-17 12:24
 */
public class SwapPairs {
    public ListNode swapPairs(ListNode head) {
        if (head == null) return null;
        if (head.next == null) return head;
        ListNode root = new ListNode();
        root.next = head;
        ListNode pre = root, cur = root.next;
        while (cur != null && cur.next != null) {
            ListNode next = cur.next;
            ListNode nextNext = next.next;
            pre.next = next;
            next.next = cur;
            cur.next = nextNext;
            pre = cur;
            cur = cur.next;
        }
        return root.next;
    }
}
